using System;
using MediaPortal.GUI.Library;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;
using System.Xml;
using System.IO;
using System.ComponentModel;
using MediaPortal.Player;
using System.Collections.Generic;
//using MediaPortal.Utils.Services;
using MediaPortal.Dialogs;
//using Rss;
using MediaPortal.GUI.View ;



namespace OnlineVideos
{
	public class VeryFunnyAdsUtil : SiteUtilBase
	{		
		//private CookieCollection moCookies;
        //private Regex regexId = new Regex("/videos/(.+)");		
		
        public List<GUIOnlineVideos.VideoInfo> parseEpisodes(String fsUrl)
        {
            List<GUIOnlineVideos.VideoInfo> loRssItems = new List<GUIOnlineVideos.VideoInfo>();
            XmlDocument doc = new XmlDocument();
            

            doc.Load(XmlReader.Create(String.Format("http://tbs.com/veryfunnyads/getCollectionById.do?offset=0&id={0}&sort=&limit=50&daysback=",fsUrl)));

            XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);           
            XmlNodeList nodeList = doc.SelectNodes("/episodes/episode", nsMgr);

           
            XmlAttributeCollection ac;

            GUIOnlineVideos.VideoInfo loRssItem;
            //GUIListItem loListItem;
            foreach (XmlNode chileNode in nodeList)
            {                
                loRssItem = new GUIOnlineVideos.VideoInfo();

                XmlNode node = chileNode.SelectSingleNode("title",nsMgr);
                loRssItem.Title = node.InnerText;

                //node = chileNode.SelectSingleNode("link");
                //loRssItem.link = node.InnerText;

                node = chileNode.SelectSingleNode("description",nsMgr);
                loRssItem.Description = node.InnerText;

                node = chileNode.SelectSingleNode("fullSizeStillUrl",nsMgr);
                if (node != null)
                {
                    //ac = node.Attributes;
                    loRssItem.ImageUrl = node.InnerText;
                }


                node = chileNode.SelectSingleNode("segments");
                node = node.SelectSingleNode("segment");
                if (node != null)
                {
                    ac = node.Attributes;
                    loRssItem.VideoUrl = ac["id"].InnerText;
                    //loRssItem.VideoUrl = node.InnerText;
                }

                //Log.Write(loRssItem.ToString());
                //loListItem = new GUIListItem(loRssItem.title);
                //loListItem.Path = loRssItem.videoUrl;
                loRssItems.Add(loRssItem);


            }
            return loRssItems;

        }
        public List<GUIOnlineVideos.RssLink> parseCollections(String fsUrl)
        {
            List<GUIOnlineVideos.RssLink> loRssItems = new List<GUIOnlineVideos.RssLink>();
            XmlDocument doc = new XmlDocument();


            doc.Load(XmlReader.Create(fsUrl));

            XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
            XmlNodeList nodeList = doc.SelectNodes("/collections/collection", nsMgr);
                       

            GUIOnlineVideos.RssLink loRssItem;
            //GUIListItem loListItem;
            foreach (XmlNode chileNode in nodeList)
            {                
                loRssItem = new GUIOnlineVideos.RssLink();
                loRssItem.url = chileNode.Attributes["id"].InnerText;
                
                XmlNode node = chileNode.SelectSingleNode("name", nsMgr);
                loRssItem.name = node.InnerText;                
                loRssItems.Add(loRssItem);


            }
            return loRssItems;

        }
        public override List<GUIOnlineVideos.RssLink> getDynamicCategories(string lsUrl)
        {
            return parseCollections("http://www.tbs.com/veryfunnyads/getCollections.do?id=24823");

        }

		public override List<GUIOnlineVideos.VideoInfo> getVideoList(string fsUrl)
		{			
			List<GUIOnlineVideos.VideoInfo> loRssItemList = parseEpisodes(fsUrl);
			//List<GUIOnlineVideos.VideoInfo> loVideoList = new List<GUIOnlineVideos.VideoInfo>();
			//GUIOnlineVideos.VideoInfo video;			
			return loRssItemList;
		}
		protected override string  getUrl(string fsUrl)

		{
			//String lsSessionId = getSessionId(foVideo.VideoUrl,foSite);
			//String lsUrl = "http://youtube.com/get_video?video_id=" +foVideo.VideoUrl  + "&t=" + lsSessionId;
			//Log.Info("youtube video url={0}", lsUrl);
			//return lsUrl+"&txe=.flv";
            String lsHtml = getHTMLData(String.Format("http://www.tbs.com/veryfunnyads/getPlaylistById.do?id={0}", fsUrl));
            Regex regex = new Regex("<url>([^<]*)");
            Match loMatch = regex.Match(lsHtml);
            String lsUrl = "";
            if (loMatch.Success)
            {
                //Console.WriteLine("success:"+loMatch.Groups[1].Value);
                lsUrl = loMatch.Groups[1].Value;
                        
            }
            return lsUrl;
		}


			public override string getSiteId()
			{
				return "27";
			}
		
	}
}
